﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Logistics.Utility
{
    /// <summary>
    /// 缓存管理
    /// 将用户凭证、令牌的关系数据存放于Cache中
    /// </summary>
    public class CacheManager
    {

        private volatile static CacheManager instance = null;
        private static object locker = new Object();
        public static CacheManager Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null)
                        {
                            instance = new CacheManager();
                        }
                    }
                }
                return instance;
            }
        }

        /// <summary>
        /// 初始化数据结构
        /// </summary>
        /// <remarks>
        /// ----------------------------------------------------
        /// | token(令牌) | info(用户凭证) | timeout(过期时间) |
        /// |--------------------------------------------------|
        /// </remarks>
        private static void cacheInit(string token, object obj, double timeout)
        {
            if (HttpContext.Current.Cache[token] == null)
            {
                if(timeout==0||timeout==null)
                {
                    timeout = double.Parse(System.Configuration.ConfigurationManager.AppSettings["timeout"]);
                }

                //HttpContext.Current.Cache.Insert(UserName, dt, null, DateTime.Now.AddMinutes(timeout), TimeSpan.Zero);//TimeSpan.Zero表示不使用平滑过期策略
                //HttpContext.Current.Cache.Insert(UserName, dt, null, DateTime.MaxValue, TimeSpan.FromMinutes(timeout));//表示缓存连续30分钟没有访问就过期。
                HttpContext.Current.Cache.Insert(token, obj, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(timeout));//表示缓存连续30分钟没有访问就过期。
            }
        }

        /// <summary>
        /// 判断令牌是否存在
        /// </summary>
        /// <param name="token">令牌</param>
        /// <returns></returns>
        public static bool TokenIsExist(string token)
        {
            object dt = HttpContext.Current.Cache[token];
            if (dt==null)
                return false;
            else
                return true;
        }

        /// <summary>
        /// 通过令牌获取缓存的值
        /// </summary>
        /// <param name="token">令牌</param>
        /// <returns></returns>
        public static object GetTokenValue(string token)
        {
            object dt = HttpContext.Current.Cache[token];
            return dt;
        }

        /// <summary>
        /// 判断令牌是否存在
        /// </summary>
        /// <param name="token">令牌</param>
        /// <returns></returns>
        public static bool TokenClear(string token)
        {

           object obj= HttpContext.Current.Cache.Remove(token);
           if (obj == null)
               return false;
           else
               return true;
        }

        /// <summary>
        /// 更新令牌过期时间
        /// </summary>
        /// <param name="token">令牌</param>
        /// <param name="time">过期时间</param>
        public static void TokenTimeUpdate(string token, DateTime time)
        {
           

            DataTable dt = (DataTable)HttpContext.Current.Cache["CERT"];
            DataRow[] dr = dt.Select("token = '" + token + "'");
            if (dr.Length > 0)
            {
                dr[0]["timeout"] = time;
            }
        }

        /// <summary>
        /// 添加令牌
        /// </summary>
        /// <param name="token">令牌</param>
        /// <param name="info">凭证</param>
        /// <param name="timeout">过期时间</param>
        public static void TokenInsert(string token,object info, double timeout=0)
        {

            if (TokenIsExist(token))
            {
  
                HttpContext.Current.Cache[token] = info;//凭证存则替换缓存内容
                //FieldValueCollection.Instance[token] = username;
            }
            else
            {
                //FieldValueCollection.Instance.Add(token, username);
                cacheInit(token, info, timeout);
            }
        }

    }//end class
}
